package medium

type TreeNode114 struct {
	Val   int
	Left  *TreeNode114
	Right *TreeNode114
}

func flatten(root *TreeNode114) {
	if root == nil {
		return
	}

	pre := new(TreeNode114)
	var inorder func(root *TreeNode114)
	inorder = func(root *TreeNode114) {
		if root == nil {
			return
		}

		l, r := root.Left, root.Right
		root.Left, root.Right = nil, nil
		pre.Right = root
		pre = pre.Right
		inorder(l)
		inorder(r)
	}

	inorder(root)
}
